<template>
  <div>
    <el-form ref="form" :model="roleInfo" label-width="80px" inline size="mini">
      <el-form-item label="角色名称">
        <el-input
          v-model="roleInfo.name"
          placeholder="请输入角色名称"
        ></el-input>
      </el-form-item>

      <!-- <el-form-item label="状态">
        <el-select v-model="searchForm.desc" placeholder="请选择状态">
          <el-option label="启动" :value="true"></el-option>
          <el-option label="禁用" :value="false"></el-option>
        </el-select>
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" class="el-icon-search" @click="searchRole"
          >查询</el-button
        >
        <el-button type="success" class="el-icon-folder-add" @click="addRole()"
          >新增角色</el-button
        >
      </el-form-item>
    </el-form>
    <!-- 添加角色对话框 -->
    <el-dialog
      :title="desc === 'add' ? '添加角色' : '修改角色'"
      :visible.sync="dialogVisible"
      width="700px"
    >
      <div>
        <el-form
          ref="roleForm"
          :model="roleForm"
          label-width="80px"
          :rules="rules"
        >
          <el-row>
            <el-col :span="12"
              ><el-form-item label="角色名称" prop="name">
                <el-input
                  v-model="roleForm.name"
                  placeholder="请输入角色名称"
                ></el-input> </el-form-item
            ></el-col>
            <el-col :span="24"
              ><el-form-item label="角色描述">
                <el-input
                  type="textarea"
                  v-model="roleForm.code"
                ></el-input> </el-form-item
            ></el-col>
            <el-col :span="24">
              <el-form-item label="菜单权限">
                <el-checkbox-group v-model="roleForm.type">
                  <el-checkbox
                    label="展开/折叠"
                    @change="open($event)"
                  ></el-checkbox>
                  <el-checkbox
                    label="全选/全不选"
                    @change="all($event)"
                  ></el-checkbox>
                  <el-checkbox
                    label="父子联动"
                    @change="handleCheckedTreeConnect($event)"
                  ></el-checkbox>
                </el-checkbox-group>
                <el-tree
                  :check-strictly="concat"
                  :data="tree"
                  show-checkbox
                  :default-expand-all="false"
                  node-key="id"
                  ref="tree"
                  highlight-current
                  :props="defaultProps"
                >
                </el-tree>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <div slot="footer">
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 角色列表 -->
    <el-table :data="userInfoList" style="width: 100%" border>
      <el-table-column width="100" align="center" type="index" label="序号">
      </el-table-column>
      <el-table-column
        align="center"
        :prop="item.info"
        :label="item.label"
        width="width"
        v-for="(item, index) in data"
        :key="index"
      >
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="{ row }">
          <el-button type="text" class="el-icon-edit" @click="editRole(row)"
            >修改</el-button
          >
          <el-popconfirm
            @onConfirm="deleRole(row)"
            confirm-button-text="好的"
            cancel-button-text="取消"
            icon="el-icon-info"
            icon-color="red"
            title="确定删除吗？"
            style="margin-left: 10px"
          >
            <el-button type="text" class="el-icon-delete" slot="reference"
              >删除</el-button
            >
          </el-popconfirm>
        </template>
      </el-table-column>
      <!-- 无数据 -->
      <template slot="empty">
        <div class="noData" v-show="userInfoList.length === 0">
          <img src="@/assets/images/noData.png" />
          <p>暂无数据</p>
        </div>
      </template>
    </el-table>
    <Pagination
      :total="total"
      @pagination="handleSizeChange"
      :page.sync="roleInfo.pageNum"
    ></Pagination>
  </div>
</template>

<script>
import { session } from "@/utils/auth";
import {
  roleList,
  saveAndUpdate,
  deleteRole,
  updataMenus,
  menusList,
} from "@/api/system-setup/role-management";
export default {
  data() {
    return {
      roleId: 0,
      concat: true,
      desc: "",
      dialogVisible: false,
      total: 0,
      //添加角色
      roleForm: {
        name: "",
        code: "",
        type: [],
      },
      rules: {
        name: [
          { required: true, message: "请输入角色名称", trigger: "blur" },
          {
            min: 2,
            max: 10,
            message: "长度在 2 到 10 个字符",
            trigger: "blur",
          },
        ],
      },
      //查询
      roleInfo: {
        code: "", //角色名称
        createTime: "",
        id: 0,
        name: "",
        pageNum: 1, //当前页
        pageSize: 20, //每页条数
        remark: "", //备注
        updateTime: "",
      },
      userInfoList: [],
      data: [
        {
          label: "角色名称",
          info: "name",
        },
        // {
        //   label: "角色状态",
        // },
        {
          label: "角色描述",
          info: "code",
        },
        {
          label: "创建时间",
          info: "createTime",
        },
        {
          label: "更新时间",
          info: "updateTime",
        },
      ],
      tree: [],
      defaultProps: {
        children: "children",
        label: "name",
      },
    };
  },
  created() {
    this.getRoleList();
  },
  methods: {
    //查询按钮
    async searchRole() {
      this.roleInfo.pageNum = 1;
      this.getRoleList();
    },
    async getRoleList() {
      let res = await roleList(this.roleInfo);
      if (res.code == 200) {
        this.userInfoList = res.data.records;
        this.total = res.data.total;
      }
    },
    //新增角色按钮
    addRole() {
      this.desc = "add";
      this.dialogVisible = true;
      this.$nextTick(() => {
        this.$refs.roleForm.clearValidate()
      })
      this.roleForm = {
        name: "",
        code: "",
        type: [],
      };
      this.tree = session.get("menuList");
    },
    //对话框确定按钮
    confirm() {
      this.$refs.roleForm.validate(async (valid) => {
        if (valid) {
          this.dialogVisible = false;
          let ids = this.$refs.tree.getCheckedKeys();
          if (this.desc === "add") {
            this.roleForm.id = 0;
          } else {
            //新增时不调该接口
            this.roleForm.id = this.roleId;

            let res = await updataMenus({ id: this.roleId, data: ids });
            if (res.code === 200) {
            }
          }

          this.roleForm.menuIds = ids;
          let res = await saveAndUpdate(this.roleForm);
          //清空
          if (res.code == 200) {
            this.getRoleList();
          }
        } else {
          return false;
        }
      });
    },
    close() {
      this.dialogVisible = false;
      this.roleForm = {
        name: "",
        code: "",
        type: [],
      };
    },
    handleSizeChange(val) {
      this.roleInfo.pageNum = val.page;
      this.roleInfo.pageSize = val.limit;
      this.getRoleList();
    },
    //修改角色
    async editRole(row) {
      this.desc = "edit";
      this.roleId = row.id;
      this.dialogVisible = true;
      this.roleForm = {
        code: row.code,
        name: row.name,
        type: [],
      };
      this.tree = session.get("menuList");
      let res = await menusList(row.id);
      if (res.code == 200) {
        const roleId = res.menuIdList;
        this.$refs.tree.setCheckedKeys(roleId);
      }
    },
    //删除角色
    async deleRole(row) {
      let res = await deleteRole([row.id]);
      if (res.code == 200) {
        this.userInfoList.length > 1
          ? this.roleInfo.pageNum
          : this.roleInfo.pageNum--;
        this.getRoleList();
      }
    },
    open(event) {
      if (event) {
        for (let i = 0; i < this.tree.length; i++) {
          this.$refs.tree.store.nodesMap[this.tree[i].id].expanded = true;
        }
      } else {
        for (let i = 0; i < this.tree.length; i++) {
          this.$refs.tree.store.nodesMap[this.tree[i].id].expanded = false;
        }
      }
    },
    all(event) {
      if (event) {
        this.$refs.tree.setCheckedNodes(this.tree);
      } else {
        this.$refs.tree.setCheckedKeys([]);
      }
    },
    handleCheckedTreeConnect(event) {
      if (event) {
        this.concat = false;
      } else {
        this.concat = true;
      }
    },
  },
};
</script>

<style lang="scss" scope="scope">
.el-tree {
  border: 1px solid rgb(229, 230, 231);
}
</style>
